# *-* coding:utf8 *-*

from flask import session
from info import ceate_app, db
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from info.models import User

app = ceate_app('development')

Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)


@manager.option('-n', '--name', dest='username')
@manager.option('-p', '--password', dest='password')
def CreateSuperAdmin(username, password):

    if not all([username, password]):
        print('1')

    user = User()
    user.mobile = username
    user.nick_name = username
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as ret:
        db.session.rollback()
        print(ret)

    session['user_id'] = user.id

    print('ss')

if __name__ == '__main__':
    manager.run()
